<?php
if(isset($_SESSION['which']) and $_SESSION['which'] == 'score')
{
	$student_id = $_SESSION['student_id'];

	// list all subjects for specified student_id
	$sql = "SELECT a.mmh, ten_mh, phan_tram_kiem_tra, diem_kiem_tra, diem_thi1, diem_thi2, hoc_ky
			FROM diem a INNER JOIN mon_hoc b ON a.mmh = b.mmh		
			WHERE mssv = '$student_id'			
			ORDER BY a.mmh, b.ten_mh
			LIMIT $offset, $num_of_rows";
	$result = mysqli_query($link, $sql);
	if(!$result)
	{
		$error = 'Error fetching student to display';
		include '/includes/error.html.php';
		exit();
	}

	$scores = array();
	while($row = mysqli_fetch_array($result))
	{
		$scores[] = array(
						  'subject_id' => $row['mmh'],
						  'subject_name' => $row['ten_mh'],					  
						  'test_percentage' => $row['phan_tram_kiem_tra'],
						  'test_score' => $row['diem_kiem_tra'],
						  'exam_score1' => $row['diem_thi1'],					  
						  'exam_score2' => $row['diem_thi2'],
						  'term' => $row['hoc_ky'] 
		);
	}

	// store scores
	$_SESSION['scores'] = $scores;
}

if(isset($_SESSION['which']) and $_SESSION['which'] == 'student')
{
	// Display student list
	$sql = "SELECT *
			FROM sinh_vien 
			ORDER BY mssv 
			LIMIT $offset, $num_of_rows";

	$result = mysqli_query($link, $sql);
	if (!$result)
	{
		$error = 'Error fetching students from database!';
		include './includes/error.html.php';
		exit();
	}

	$students = array();
	while ($row = mysqli_fetch_array($result))
	{
		$students[] = array('student_id' => $row['mssv'],
						  'name' => $row['ho_ten'],
						  'birthday' => $row['ngay_sinh'],
						  'edu_system' => $row['he_dao_tao'],
						  'branch' => $row['nganh_hoc'],
						  'course' => $row['khoa_hoc']);
	}
}

if(isset($_SESSION['which']) and $_SESSION['which'] == 'subject')
{
	$sql = "SELECT *
			FROM mon_hoc 
			ORDER BY mmh 
			LIMIT $offset, $num_of_rows";
	$result = mysqli_query($link, $sql);
	if(!$result)
	{
		$error = 'Error fetching subjects from database';
		include './includes/error.html.php';
		exit();
	}

	$subjects = array();
	while($row = mysqli_fetch_array($result))
	{
		$subjects[] = array('subject_id' => $row['mmh'],
						'subject_name' => $row['ten_mh'],
						'test_percentage' => $row['phan_tram_kiem_tra']);
	}
}